package manager_broker

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/request"
	"hft/Http/Services/manager_broker"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type Broker struct{}

func (*Broker) GetBrokerList(c *gin.Context) {

	var paginate request.BrokerPage

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := manager_broker.BrokerService.GetBrokerList(claims.BaseClaims, &paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*Broker) CreateBroker(c *gin.Context) {
	var data *admin.Account

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.CreateBroker(data, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Broker) EditBroker(c *gin.Context) {
	var data *admin.Account

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.EditBroker(data, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Broker) ReplaceLouPan(c *gin.Context) {
	var data *request.BrokerLouPan

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.ReplaceLouPan(data.AccountId, data.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Broker) GetAllLouPan(c *gin.Context) {
	var lou request.SearchLou

	c.ShouldBind(&lou)

	fmt.Println(tools.JsonParse(&lou))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.Fail(c)
		return
	}

	list, err := manager_broker.BrokerService.GetAllLouPan(&lou, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*Broker) Delete(c *gin.Context) {

	var IDS *request.IDS

	c.ShouldBindJSON(&IDS)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.Delete(IDS.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Broker) Status(c *gin.Context) {

	var data *admin.Account

	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.Status(data, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Broker) SendGold(c *gin.Context) {
	var gold *request.SendGold

	c.ShouldBindJSON(&gold)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = manager_broker.BrokerService.SendGold(gold.ID, gold.Num, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

var BrokerController = new(Broker)
